package cn.wjc.demo.parttwo.service.impl;

import cn.wjc.demo.common.entity.Partner;
import cn.wjc.demo.common.page.PageQuery;
import cn.wjc.demo.parttwo.mapper.IPartnerMapper;
import cn.wjc.demo.parttwo.service.IPartnerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PartnerServiceImpl implements IPartnerService {


    private final IPartnerMapper partnerMapper;

    @Override
    public  IPage<Partner> queryPartner(PageQuery pageQuery, Partner partner) {
        Page<Partner> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<Partner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(partner.getPName()), Partner::getPName, partner.getPName())
                .like(StringUtils.hasText(partner.getPSex()), Partner::getPSex, partner.getPSex())
                .between(partner.getPBirthStart() != null && partner.getPBirthEnd() != null, Partner::getPBirth, partner.getPBirthStart(), partner.getPBirthEnd())
                .eq(partner.getPAge() != null, Partner::getPAge, partner.getPAge());


        return partnerMapper.selectPage(page, queryWrapper);
    }
}
